// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-11 17:42:22
// 生成路径: internal/app/system/logic/schools.go
// 生成人：王中阳
// desc:学校
// company:云南奇讯科技有限公司
// ==========================================================================

package schools

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
	"github.com/xuri/excelize/v2"
)

func init() {
	service.RegisterSchools(New())
}

func New() service.ISchools {
	return &sSchools{}
}

type sSchools struct{}

func (s *sSchools) List(ctx context.Context, req *model.SchoolsSearchReq) (listRes *model.SchoolsSearchRes, err error) {
	listRes = new(model.SchoolsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Schools.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.Schools.Columns().Id+" = ?", req.Id)
		}
		if req.Province != "" {
			m = m.Where(dao.Schools.Columns().Province+" = ?", req.Province)
		}
		if req.City != "" {
			m = m.Where(dao.Schools.Columns().City+" = ?", req.City)
		}
		if req.Department != "" {
			m = m.Where(dao.Schools.Columns().Department+" = ?", req.Department)
		}
		if req.Level != "" {
			m = m.Where(dao.Schools.Columns().Level+" = ?", req.Level)
		}
		if req.Schoolname != "" {
			m = m.Where(dao.Schools.Columns().Schoolname+" like ?", "%"+req.Schoolname+"%")
		}
		if req.Type != "" {
			m = m.Where(dao.Schools.Columns().Type+" = ?", req.Type)
		}
		if req.Code != "" {
			m = m.Where(dao.Schools.Columns().Code+" = ?", gconv.Int(req.Code))
		}
		if req.Show != "" {
			m = m.Where(dao.Schools.Columns().Show+" = ?", gconv.Int(req.Show))
		}
		if req.Lat != "" {
			m = m.Where(dao.Schools.Columns().Lat+" = ?", req.Lat)
		}
		if req.Lng != "" {
			m = m.Where(dao.Schools.Columns().Lng+" = ?", req.Lng)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.Schools.Columns().CreatedAt+" >=? AND "+dao.Schools.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.SchoolsListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.SchoolsListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.SchoolsListRes{
				Id:         v.Id,
				Province:   v.Province,
				City:       v.City,
				Department: v.Department,
				Level:      v.Level,
				Schoolname: v.Schoolname,
				Type:       v.Type,
				Code:       v.Code,
				Show:       v.Show,
				Lat:        v.Lat,
				Lng:        v.Lng,
				CreatedAt:  v.CreatedAt,
			}
		}
	})
	return
}

func (s *sSchools) GetExportData(ctx context.Context, req *model.SchoolsSearchReq) (listRes []*model.SchoolsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Schools.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.Schools.Columns().Id+" = ?", req.Id)
		}
		if req.Province != "" {
			m = m.Where(dao.Schools.Columns().Province+" = ?", req.Province)
		}
		if req.City != "" {
			m = m.Where(dao.Schools.Columns().City+" = ?", req.City)
		}
		if req.Department != "" {
			m = m.Where(dao.Schools.Columns().Department+" = ?", req.Department)
		}
		if req.Level != "" {
			m = m.Where(dao.Schools.Columns().Level+" = ?", req.Level)
		}
		if req.Schoolname != "" {
			m = m.Where(dao.Schools.Columns().Schoolname+" like ?", "%"+req.Schoolname+"%")
		}
		if req.Type != "" {
			m = m.Where(dao.Schools.Columns().Type+" = ?", req.Type)
		}
		if req.Code != "" {
			m = m.Where(dao.Schools.Columns().Code+" = ?", gconv.Int(req.Code))
		}
		if req.Show != "" {
			m = m.Where(dao.Schools.Columns().Show+" = ?", gconv.Int(req.Show))
		}
		if req.Lat != "" {
			m = m.Where(dao.Schools.Columns().Lat+" = ?", req.Lat)
		}
		if req.Lng != "" {
			m = m.Where(dao.Schools.Columns().Lng+" = ?", req.Lng)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.Schools.Columns().CreatedAt+" >=? AND "+dao.Schools.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&listRes)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
	})
	return
}

func (s *sSchools) Import(ctx context.Context, file *ghttp.UploadFile) (err error) {
	if file == nil {
		err = errors.New("请上传数据文件")
		return
	}
	var data []do.Schools
	err = g.Try(ctx, func(ctx context.Context) {
		f, err := file.Open()
		liberr.ErrIsNil(ctx, err)
		defer f.Close()
		exFile, err := excelize.OpenReader(f)
		liberr.ErrIsNil(ctx, err)
		defer exFile.Close()
		rows, err := exFile.GetRows("Sheet1")
		liberr.ErrIsNil(ctx, err)
		if len(rows) == 0 {
			liberr.ErrIsNil(ctx, errors.New("表格内容不能为空"))
		}
		d := make([]interface{}, len(rows[0]))
		data = make([]do.Schools, len(rows)-1)
		for k, v := range rows {
			if k == 0 {
				continue
			}
			for kv, vv := range v {
				d[kv] = vv
			}
			data[k-1] = do.Schools{
				Province:   d[0],
				City:       d[1],
				Department: d[2],
				Level:      d[3],
				Schoolname: d[4],
				Type:       d[5],
				Code:       gconv.Int64(d[6]),
				Show:       gconv.Int64(d[7]),
				Lat:        d[8],
				Lng:        d[9],
				CreatedAt:  gconv.GTime(d[10]),
			}
		}
		if len(data) > 0 {
			err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
				_, err = dao.Schools.Ctx(ctx).Batch(500).Insert(data)
				return err
			})
			liberr.ErrIsNil(ctx, err)
		}
	})
	return
}

func (s *sSchools) GetById(ctx context.Context, id int) (res *model.SchoolsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Schools.Ctx(ctx).WithAll().Where(dao.Schools.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sSchools) Add(ctx context.Context, req *model.SchoolsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Schools.Ctx(ctx).Insert(do.Schools{
			Province:   req.Province,
			City:       req.City,
			Department: req.Department,
			Level:      req.Level,
			Schoolname: req.Schoolname,
			Type:       req.Type,
			Code:       req.Code,
			Show:       req.Show,
			Lat:        req.Lat,
			Lng:        req.Lng,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sSchools) Edit(ctx context.Context, req *model.SchoolsEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Schools.Ctx(ctx).WherePri(req.Id).Update(do.Schools{
			Province:   req.Province,
			City:       req.City,
			Department: req.Department,
			Level:      req.Level,
			Schoolname: req.Schoolname,
			Type:       req.Type,
			Code:       req.Code,
			Show:       req.Show,
			Lat:        req.Lat,
			Lng:        req.Lng,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sSchools) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Schools.Ctx(ctx).Delete(dao.Schools.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
